package com.ks.composestudy

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.ks.composestudy.ui.BottomBar
import com.ks.composestudy.ui.ChatList
import com.ks.composestudy.ui.ChatPage
import com.ks.composestudy.ui.Home
import com.ks.composestudy.ui.theme.composestudyTheme

class SimpleActivity : ComponentActivity() {
    private val viewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            composestudyTheme {
                Home(viewModel)
                ChatPage(viewModel)
            }
        }
    }

    override fun onBackPressed() {
        if (!viewModel.endChat()) {
            super.onBackPressed()
        }
    }
}

@OptIn(ExperimentalPagerApi::class)
@Preview(showBackground = true)
@Composable
fun SimpleActivityPreview() {
    composestudyTheme {
        Column {
            val viewModel: MainViewModel = viewModel()
            HorizontalPager(4, Modifier.weight(1f)) {
                when (it) {
                    0 -> ChatList(viewModel.chats)
                }
            }
            BottomBar(viewModel.selectedTab) {
                viewModel.selectedTab = it
            }
        }
    }
}

